package info.wegosoft.android.activity;

import info.wegosoft.R;
import info.wegosoft.android.model.MBlog;
import info.wegosoft.android.model.Platform;
import info.wegosoft.android.qauth.sina.OAuthConstant;
import info.wegosoft.android.qauth.sina.Weibo;
import info.wegosoft.android.qauth.sina.WeiboException;
import info.wegosoft.android.qauth.tencent.OAuth;
import info.wegosoft.android.qauth.tencent.QWeiboAsyncApi;
import info.wegosoft.android.qauth.tencent.QWeiboType.ResultType;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

/**  
 * Filename:    RepostMBlogActivity.java 
 * Package:     info.wegosoft.android.activity 
 * Description: 转发微博Activity
 * Copyright:   Copyright (c) wegosoft.info 2011  
 * @author:     李辉 
 * email:       fzlihui@gmail.com
 * @version:    1.0  
 * Create at:   2011-6-21 下午08:27:55  
 */
public class RepostMBlogActivity extends Activity {

	private MBlog mblog;
	private EditText microBlog_ed;
	private TextView text_limit;
	private ProgressDialog progressDialog = null;
	private int platformType;
	//转发信息的分隔符，新浪未//腾讯为||
	private String separator;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.write_mblog);
		
		mblog = (MBlog) getIntent().getSerializableExtra("mblog");
		
		platformType = this.getSharedPreferences("platform",Context.MODE_PRIVATE).getInt("type", -1);
		
		switch (platformType) {
			case Platform.SINA_PLATFORM:
				 
				 separator = "//";
				 
				 break;
			case Platform.TENCENT_PLATFORM:
				 
				 separator = "||";
				 
				 break;	
			default:
				 separator = "//";
				
				 break;
		}
		
		//字数限制提醒
        text_limit = (TextView) findViewById(R.id.text_limit);
        
		microBlog_ed = (EditText) findViewById(R.id.microBlog_ed);

		microBlog_ed.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			
			public void afterTextChanged(Editable s) {

				int charNum = microBlog_ed.getText().length();
				
				text_limit.setText("" + (140 - charNum));
			}
		});

		if(mblog.getSourceMBlog()!=null){
			
			microBlog_ed.setText(separator + " @" + mblog.getScreen_name() + ": " + mblog.getText());
		
		}

		ImageButton insertImageButton = (ImageButton) findViewById(R.id.insert_pic_btn);;
		
		insertImageButton.setVisibility(View.GONE);

		TextView titleTV = (TextView) findViewById(R.id.titleTV);
		
		titleTV.setText(getResources().getString(R.string.forward_title));
		
		Button sendBtn = (Button) findViewById(R.id.sendBtn);
		
		sendBtn.setText(getResources().getString(R.string.send_btn_forward));
		
		sendBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				final String text = microBlog_ed.getText().toString().trim();

				progressDialog = ProgressDialog.show(RepostMBlogActivity.this,null,"转发中",true);
				
				new Thread(){

					public void run() {
						
						Looper.prepare(); 
						
						switch (platformType) {
							case Platform.SINA_PLATFORM:
								 
								 repostToSina(text);
								 
								 break;
							case Platform.TENCENT_PLATFORM:
								 
								 repostToTencent(text);
								 
								 break;	
							default:
								 break;
						}
						
						Looper.loop();	
					}
					
				}.start();

			}
		});
        
        int charNum = microBlog_ed.getText().length();
		
		text_limit.setText("" + (140 - charNum));
	}
	
	private void repostToSina(String text){
		
		try {

			Weibo weibo = OAuthConstant.getInstance().getWeibo();	

			weibo.repost(mblog.getId(), text, 0);

			progressDialog.dismiss();
			
			Toast.makeText(RepostMBlogActivity.this, "转发成功", Toast.LENGTH_SHORT).show();
			
			returnToDetailMBlogActivity();
			
		} catch (WeiboException e) {
			
			progressDialog.dismiss();
			
			Toast.makeText(RepostMBlogActivity.this, "转发失败", Toast.LENGTH_SHORT).show();
			
			e.printStackTrace();
			
			returnToDetailMBlogActivity();
		}
		
	}
	
	private void repostToTencent(String text){
		
		SharedPreferences preference = getSharedPreferences("tencent_qauth",Context.MODE_PRIVATE);
		
		QWeiboAsyncApi api = new QWeiboAsyncApi();
		
		if(api.repostMsg(OAuth.AppKey, OAuth.AppSecret,preference.getString("AccessToken", null),preference.getString("AccessTokenSecret", null), text, ResultType.ResultType_Json, mblog.getId())){
			
			progressDialog.dismiss();
			
			Toast.makeText(RepostMBlogActivity.this, "转发成功", Toast.LENGTH_SHORT).show();
			
			returnToDetailMBlogActivity();
			
		}else{
			
			progressDialog.dismiss();
			
			Toast.makeText(RepostMBlogActivity.this, "转发失败", Toast.LENGTH_SHORT).show();
			
			returnToDetailMBlogActivity();
		}
	}
	
	private void returnToDetailMBlogActivity(){
		
		Intent intent = new Intent();
		
		Bundle bundle = new Bundle();
		
		bundle.putSerializable("mblog", mblog);
		
		intent.putExtras(bundle);
		
		intent.setClass(RepostMBlogActivity.this, DetailMBlogActivity.class);
		
		RepostMBlogActivity.this.finish();
		
		startActivity(intent);
		
	}
}
